import {
    httpAction,
    getAction
} from '@/http/axios.js';
import E from 'wangeditor';
export const JVxeTableModelMixin = {
    data() {
        return {
            title: '操作',
            visible: false,
            confirmLoading: false,
            scrolling: true,
            ruleForm: {
            },
            uploadfileurl: this.$url.baseURL + '/upload/uploadfile',
        }
    },
    methods: {
        submitForm() {
            var arr = this.$refs.tree.getCheckedKeys();
            var halfarr = this.$refs.tree.getHalfCheckedKeys();
            halfarr.forEach((item, index) => {
                arr.push(item)
            })

            this.$confirm('确认保存吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let url = this.url.edit
                httpAction(url,{
                    roleid: this.ruleForm.id,
                    permissionStr: arr.toString()
                }).then(resp => {
                    if (resp.code != 0) {
                        this.$message.error(resp.msg);
                    } else {
                        this.$message({
                            type: 'success',
                            message: '已保存'
                        });
                        this.$emit('ok')
                    }
                })
            }).catch(() => {
            });
        },
        getTreeList() {
            let url = this.url.list
            httpAction(url,{
                roleid: this.ruleForm.id
            }).then(resp => {
                if (resp.code != 0) {
                    this.$message.error(resp.msg);
                } else {
                    this.data = resp.data;
                    this.getRolePermissionTreeCheckedKeys();
                    this.loading = false;
                }
            })
        },
        getRolePermissionTreeCheckedKeys() {
            let url = this.url.treeCheckedList
            httpAction(url,{
                roleid: this.ruleForm.id
            }).then(resp => {
                if (resp.code != 0) {
                    this.$message.error(resp.msg);
                } else {
                    this.checkedkeys = resp.data;
                    //注： 此方法回显 会自动选中父节点，从而自动选中其他未选中的兄弟节点；解决方案如下，
                    // this.$refs.tree.setCheckedKeys(this.checkedkeys);

                    //利用tree组件渲染后带有的isLeaf(是否为叶子节点)属性，如果为叶子节点就选中。这样利用tree的API就实现了正确的回显效果
                    var that = this;
                    this.checkedkeys.forEach((i, n) => {
                        var node = that.$refs.tree.getNode(i);
                        if (node.isLeaf) {
                            that.$refs.tree.setChecked(node, true);
                        }
                    });
                }
            })
        },
        //清空数据
        resetForm() {
            this.$refs['ruleForm'].resetFields();
        },
        /** 当点击了编辑（修改）按钮时调用此方法 */
        edit(record) {
            console.log(record);
            this.visible = true
            this.ruleForm = Object.assign({}, record)
            this.getTreeList()
        },

        /** 关闭按钮点击事件 */
        handleCancel() {
            this.visible = false
            this.$emit('close')
        },

    }
}
